/**
 *添加行政区划
 */
'use strict';

const Transaction = require("../../libs/DB/Transaction");
const operationRecord = require("../sys/operationRecordAction");
module.exports = async function (params) {
    let pointList = params.pointList
    let transaction = new Transaction();
    try {
        await transaction.beginTransaction();
        await transaction.query('DELETE FROM domain_coordinate WHERE domain_id = ?',[params.domainId]);

        for (const domain of pointList) {
            const index = pointList.indexOf(domain);
            let insertObj = {
                domain_id:params.domainId,
                lng:domain[0],
                lat:domain[1],
                index:index,
            }
            await transaction.query('INSERT INTO domain_coordinate SET ?',[insertObj]);
        }
        await transaction.commit();
        await operationRecord(params);
        return {success: true};
    }catch (e){
        await transaction.rollback();
        return { success: false,message:e.message };
    }
};